import django_filters
from main.filters import NumberInFilter
from main.models import Reclamation
from main.permissions import DjangoModelFullPermissions
from rest_framework import serializers, viewsets


class ReclamationSerializer(serializers.ModelSerializer):
    warranty = serializers.PrimaryKeyRelatedField(
        read_only=True, required=False
    )
    contract = serializers.PrimaryKeyRelatedField(
        read_only=True, required=False
    )
    attachs = serializers.SerializerMethodField(read_only=True, required=False)

    def get_attachs(self, obj):
        return obj.attachs.count()

    class Meta:
        model = Reclamation
        fields = (
            "id",
            "number_in_program",
            "discovered_on",
            "remediation_date",
            "note",
            "created_on",
            "updated_on",
            "warranty",
            "contract",
            "attachs",
        )


class ReclamationFilter(django_filters.FilterSet):
    ids = NumberInFilter(field_name="id", lookup_expr="in")

    class Meta:
        model = Reclamation
        fields = ("id",)


class ReclamationViewSet(viewsets.ModelViewSet):
    queryset = Reclamation.objects.all()
    serializer_class = ReclamationSerializer
    permission_classes = (DjangoModelFullPermissions,)
    filterset_class = ReclamationFilter
